package com.geekstudy.week5.controller;

import com.geekstudy.week5.entity.GeekStudy;
import com.geekstudy.week5.service.Exercise10HikariService;
import com.geekstudy.week5.service.Exercise10PrepareStatementService;
import com.geekstudy.week5.service.Exercise10PrimitiveJdbcService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
public class Exercise10Controller {

    final Logger logger = LoggerFactory.getLogger(Exercise10Controller.class);

    @Autowired
    private Exercise10PrimitiveJdbcService exercise10PrimitiveJdbcService;

    @Autowired
    private Exercise10PrepareStatementService exercise10PrepareStatementService;

    @Autowired
    private Exercise10HikariService exercise10HikariService;

    @RequestMapping("study/queryAll")
    public Map<String, Object> queryAllStudy() {
        Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
        try {
            List<GeekStudy> queryList = exercise10PrimitiveJdbcService.queryAll();
            resultMap.put("message", "querySuccess");
            resultMap.put("geekStudyList", queryList);
        } catch(Exception e) {
            resultMap.put("message", e.getMessage());
            logger.error("queryAllStudy exception:{} ", e);
        }
        return resultMap;
    }

    @RequestMapping("study/primitive")
    public Map<String, Object> primitive() {
        Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
        try {
            Map<String, Object> primitiveMap = exercise10PrimitiveJdbcService.primitive();
            resultMap.put("message", "primitive success");
            resultMap.putAll(primitiveMap);
        } catch(Exception e) {
            resultMap.put("message", e.getMessage());
            logger.error("primitive exception:{} ", e);
        }
        return resultMap;
    }

    @RequestMapping("study/prepareStatement")
    public Map<String, Object> prepareStatement() {
        Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
        try {
            Map<String, Object> map = exercise10PrepareStatementService.prepareStatement();
            resultMap.put("message", "prepare statement success");
            resultMap.putAll(map);
        } catch(Exception e) {
            resultMap.put("message", e.getMessage());
            logger.error("prepareStatement exception:{} ", e);
        }
        return resultMap;
    }

    @RequestMapping("study/hikari")
    public Map<String, Object> hikari() {
        Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
        try {
            Map<String, Object> map = exercise10HikariService.hikari();
            resultMap.put("message", "Hikari success");
            resultMap.putAll(map);

        } catch(Exception e) {
            resultMap.put("message", e.getMessage());
            logger.error("hikari exception:{} ", e);
        }

        return resultMap;
    }

}
